/*
 * Copyright (C) 2009 Kristopher T Babic
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, see <http://www.gnu.org/licenses>.
 */

package edu.idp.client.anim.sequence;

import gjt.Util;

import java.awt.*;
import java.net.URL;
import java.util.Vector;

/**
 *
 * @author Kristopher T Babic
 */
public class ImageSequence extends Sequence {

    private transient Vector cells = null;
    private boolean initialized = false;
    private URL[] images = null;

    public ImageSequence(Component component, URL[] images) {
        super();

        setComponent(component);

        this.images = images;
        initImages();
//    cyclesPerAnimation = infiniteCycle;
    }

    private void addImage(Component component, Image image) {
        if (currentImage == null)
            currentImage = image;

        Util.waitForImage(component, image);
        if (cells == null)
            cells = new Vector();
        cells.addElement(image);
    }

    public int getCurrentImagePosition() {
        if (cells == null) {
            if (images != null && images.length > 0)
                initImages();
            else
                return -1;
        }

        return cells.indexOf(currentImage);
    }

    private void removeImage(Image image) {
        if (cells == null) {
            if (images != null && images.length > 0)
                initImages();
        }

        cells.removeElement(image);
    }

    public String toString() {
        return "Animated Image Sequence";
    }

    public Image getFirstImage() {
        if (cells == null) {
            if (images != null && images.length > 0)
                initImages();
            else
                return null;
        }

        return (Image) cells.firstElement();
    }

    public Image getCurrentImage() {
        return currentImage;
    }

    public Image getNextImage() {
        if (cells == null) {
            if (images != null && images.length > 0)
                initImages();
            else
                return null;
        }

        int index = cells.indexOf(currentImage);
        Image image;

        if (index == cells.size() - 1)
            image = (Image) cells.elementAt(0);
        else
            image = (Image) cells.elementAt(index + 1);

        return image;
    }

    public boolean isAtLastImage() {
        return getCurrentImagePosition() == (cells.size() - 1);
    }

    protected void initImages() {
//    Component comp = getComponent();
        Toolkit tk = Toolkit.getDefaultToolkit();
        Component component = getComponent();
        for (int i = 0; i < images.length; ++i) {
            addImage(component, tk.getImage(images[i]));
        }

    }

}